#include "codec_task.h"
#include "decoder.h"
#include "encoder.h"
#include "ff.h"
#include "common_utils.h"


#define L_FRAME        80
#define BITSTREAM      10


/*解码器句柄*/
static bcg729DecoderChannelContextStruct *pDecoderChannelContext = NULL;
/*编码器句柄 */
static bcg729EncoderChannelContextStruct *pEncoderChannelContext = NULL;


uint8_t  enc_buf[2048];
lwrb_t   g_lwrb_enc;

uint8_t  dec_buf[2048];
lwrb_t   g_lwrb_dec;

uint8_t  rw_buf[2048];
lwrb_t   g_lwrb_rw;

char* audioname="0:/audio.wav";
/**
 * 函数功能：编解码任务初始化
 */
void codec_task_init(void)
{
    pEncoderChannelContext = initBcg729EncoderChannel(0);
    pDecoderChannelContext = initBcg729DecoderChannel();


    lwrb_init(&g_lwrb_enc, enc_buf, sizeof(enc_buf));
    lwrb_init(&g_lwrb_dec, dec_buf, sizeof(dec_buf));
    lwrb_init(&g_lwrb_rw, rw_buf, sizeof(rw_buf));
}

/**
 * 函数功能：编码器任务
 */
void Encoder_Handle(void)
{
    uint8_t bitStreamLength = 0;
    int16_t inputBuffer[L_FRAME] = {0};
    uint8_t bitStream[BITSTREAM] = {0};
    lwrb_sz_t buf_size=0;
    if(pEncoderChannelContext)
    {
        buf_size=lwrb_get_full(&g_lwrb_rw);
        while(buf_size>=(L_FRAME*sizeof(uint16_t)))
        {
            lwrb_read(&g_lwrb_rw,(uint8_t*)inputBuffer,(L_FRAME*sizeof(uint16_t)));
            bcg729Encoder(pEncoderChannelContext, inputBuffer, bitStream, &bitStreamLength);
            lwrb_write(&g_lwrb_enc,bitStream,BITSTREAM);
            buf_size=lwrb_get_full(&g_lwrb_rw);
        }
    }

}
/**
 * 函数功能：解码器任务
 */
void Decoder_Handle(void)
{
    uint8_t frameErasureFlag = 0;
    uint8_t SIDFrameFlag = 0;
    uint8_t rfc3389PayloadFlag = 0;
    uint8_t bitStream[BITSTREAM] = {0};
    int16_t outputBuffer[L_FRAME] = {0};
    lwrb_sz_t buf_size=0;
    if(pDecoderChannelContext)
    {
        buf_size=lwrb_get_full(&g_lwrb_enc);
        while(buf_size>=BITSTREAM)
        {

            bcg729Decoder(pDecoderChannelContext, bitStream, BITSTREAM, frameErasureFlag,
                          SIDFrameFlag, rfc3389PayloadFlag, outputBuffer);
            lwrb_write(&g_lwrb_dec,outputBuffer,L_FRAME);

            buf_size=lwrb_get_full(&g_lwrb_enc);
        }
    }
}

/**
 * 函数功能：读取语音文件任务
 */
void Audio_Read_Handle(void)
{
    static uint8_t cbStep=0;
    FRESULT frResult;
    static FIL fp;
    static uint32_t filesize=0,readOffet=0;
    static uint8_t read_buf[1024]= {0};
    uint32_t read_size=0;

    switch (cbStep)
    {
        case 0:
            frResult = f_open(&fp, audioname, FA_OPEN_EXISTING | FA_READ);

            if (frResult != FR_OK)
            {
                APP_PRINT("file create err\r\n");
                cbStep=3;
            }
            else
            {
                cbStep=1u;
            }

            break;

        case 1:
            filesize=f_size(&fp);

            if(filesize>0)
            {
                cbStep=2u;
            }
            else
            {
                cbStep=3u;
            }

            break;

        case 2:
            frResult = f_read(&fp, read_buf, sizeof(read_buf),&read_size);

            if(frResult==FR_OK)
            {
                lwrb_write(&g_lwrb_rw,read_buf,read_size);
                readOffet+=read_size;
            }
            else
            {
                cbStep=3;
            }

            if(readOffet>=filesize)
            {
                cbStep=3;
            }

            break;

        case 3:
            f_close(&fp);
            cbStep=0xff;
            break;

        default:
            break;
    }
}

/*
*函数功能：编解码任务
*/
void codec_task(void)
{
    Audio_Read_Handle();
    Encoder_Handle();
    //Decoder_Handle();
}
